#pragma once
//============================================================================
// Copyright (C) 2009-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <VxUtilLib/VxThread.h>
#include <VxNetLib/VxSktBaseMgr.h>

// from header in case not defined
#ifndef FD_SETSIZE
#define FD_SETSIZE      64
#endif /* FD_SETSIZE */

// implements a manager to manage accept sockets
class VxSktAcceptMgr : public VxSktBaseMgr 
{
public:
	//=== vars ===//
	VxThread				m_oListenThread;				// thread to listen for incoming connections
	int						m_iMgrId;						// unique id for this manager
	static int				m_iAcceptMgrCnt;				// number of managers created
	RCODE					m_rcLastError;					// last error that occurred

	U16						m_u16ListenPort;				// what port to listen on
	SOCKET					m_aoListenSkts[ FD_SETSIZE ];	// array of listen sockets
	int						m_iActiveListenSktCnt;			// number of active listening sockets

	//=== constructor ===//
	VxSktAcceptMgr();
	//=== destructor ===//
	virtual ~VxSktAcceptMgr();

	// overrides VxSktMgrBase
	virtual void			sktMgrShutdown( void );

	// overrides VxSktMgrBase
	//! make a new socket... give derived classes a chance to override
	virtual VxSktBase *		makeNewSkt( void );

	virtual RCODE			startListening( U16 u16ListenPort );
	virtual RCODE			stopListening( void );

	void					ListenForConnectionsToAccept( VxThread * poThread );

private:
	RCODE 					AcceptConnection( VxThread * poThread, SOCKET oListenSkt );

	//=== functions that should only be called by derived classes ===//
	//! handle callbacks from sockets
	//virtual	void			DoRecieveCallback( VxSktBase * poSkt );
};

